package com.atguigu.crowd.service.service;

import com.atguigu.crowd.entity.po.MemberPO;
import com.atguigu.crowd.entity.po.MemberPOExample;
import com.atguigu.crowd.mapper.MemberPOMapper;
import com.atguigu.crowd.service.api.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
// 在类上使用@Transactional(readOnly = true)针对查询操作设置事务属性
@Transactional(readOnly = true)
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberPOMapper memberPOMapper;


    @Override
    public MemberPO getMemberPOByLoginAcct(String loginAcct) {

        //封装查询条件
        MemberPOExample example = new MemberPOExample();

        MemberPOExample.Criteria criteria = example.createCriteria();


        criteria.andLoginacctEqualTo(loginAcct);

        //执行查询
        List<MemberPO> list = memberPOMapper.selectByExample(example);

        if (list==null || list.size()==0){
            return null;
        }
        //获取查询的对象
        MemberPO memberPO = list.get(0);

        return memberPO;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public void saveMember(MemberPO memberPO) {
        int i = memberPOMapper.insertSelective(memberPO);

    }
}
